﻿using Modules.VocabularyTrainer.Shared.Strategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Genius.Brain.Shared.BO;
using System.IO;
using Newtonsoft.Json.Linq;

namespace Modules.VocabularyTrainer.Impl.Strategy
{
    class VocabularyMatchRateStrategy : IVocabularyMatchRateStrategy
    {
        private JObject _database;

        public VocabularyMatchRateStrategy(String configuration)
        {
            var json = File.ReadAllText(configuration);
            _database = JObject.Parse(json);
        }

       

        public VocabularyMatchRate Rate(IList<IMatch> matches)
        {
            VocabularyMatchRate diff = 0;

            for(var i = 0;i < matches.Count;i++)
            {
                var match = matches[i];
                var ldiff = VocabularyMatchRate.Unknown;
                switch(match.MatchType)
                {
                    case MatchType.Equal:
                        ldiff = VocabularyMatchRate.None;
                        break;
                    case MatchType.EqualIgnoreCase:
                        if (i > 0)
                            ldiff = VocabularyMatchRate.WrongCase;
                        else
                            ldiff = 0;
                        break;
                    case MatchType.EqualIgnoreDiacritics:
                        ldiff = VocabularyMatchRate.WrongDiacritics;
                        break;
                    case MatchType.Missing:
                        if (match.Pattern.Type.Equals("mark"))
                            ldiff = VocabularyMatchRate.MissingMark;
                        if (match.Pattern.Type.Equals("word"))
                        {
                            //Artikel vergessen????
                            ldiff = VocabularyMatchRate.Missing;
                            if (_Is(_database["articles"], match.Pattern.Data))
                            {
                                ldiff = VocabularyMatchRate.MissingArticle;
                            }
                            if (_Is(_database["prepositions"], match.Pattern.Data))
                            {
                                ldiff = VocabularyMatchRate.MissingPreposition;
                            }
                        }
                        break;
                    case MatchType.Extra:
                        if (match.Word.Type.Equals("mark"))
                            ldiff = VocabularyMatchRate.WrongMark;
                        if (match.Word.Type.Equals("word"))
                        {
                            //Artikel zu viel???
                            ldiff = VocabularyMatchRate.Wrong;
                            if(_Is(_database["articles"], match.Word.Data))
                            {
                                ldiff = VocabularyMatchRate.WrongArticle;
                            }
                            if (_Is(_database["prepositions"], match.Word.Data))
                            {
                                ldiff = VocabularyMatchRate.WrongPreposition;
                            }
                        }
                        break;
                    case MatchType.DifferentType:
                        ldiff = VocabularyMatchRate.Wrong;
                        break;
                    case MatchType.Different:
                        if (match.Pattern.Type.Equals("mark"))
                            ldiff = VocabularyMatchRate.WrongMark;
                        if (match.Pattern.Type.Equals("word"))
                        {
                            ldiff = VocabularyMatchRate.Wrong;
                            //Endung falsch ldiff=6
                            //Artikel falsch ldiff=7
                            if(_Is(_database["articles"], match.Pattern.Data) && _Is(_database["articles"], match.Word.Data))
                            {
                                ldiff = VocabularyMatchRate.WrongArticle;
                            }
                            //Präposition falsch ldiff=8
                            if (_Is(_database["prepositions"], match.Pattern.Data) && _Is(_database["prepositions"], match.Word.Data))
                            {
                                ldiff = VocabularyMatchRate.WrongPreposition;
                            }
                        }
                        break;


                }


                //kleine Fehler verzeihen, schwere nicht.
                if (diff <= VocabularyMatchRate.SimpleErrors && diff < ldiff)
                    diff = ldiff;
                else if(diff > VocabularyMatchRate.SimpleErrors && ldiff > VocabularyMatchRate.SimpleErrors)
                {
                    diff = VocabularyMatchRate.Wrong;
                }


            }
            return diff;
        }

        private bool _Is(JToken list, string data)
        {
            foreach(var patt in list)
            {
                if (patt.Value<string>().Equals(data.ToLower()))
                    return true;
            }
            return false;
        }
    }
}
